﻿using STHPMS1.ICore.I_DAL.Repository;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ServiceStack.OrmLite;
using STHPMS1.DAL.D_Core;

namespace STHPMS1.DAL.D_Repository
{
    public class S_AxleloadRepository : DalBase, IS_AxleloadRepository
    {
        /// <summary>
        /// 一个应用区间同一个观测日期下有可能存在不同观测点数据，现只采用一个观测点数据
        /// 一个应用区间每年也只能使用一个数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="applyIds"></param>
        /// <returns></returns>
        public IEnumerable<T> GetAxleloadByApply<T>(string applyIds)
               where T : class, IS_Axleload
        {
            string applyIdWhere = "";
            if (!string.IsNullOrEmpty(applyIds)) applyIdWhere = string.Format(" and applyid in( {0} )", applyIds);
            string sql = string.Format(@"select * from (select * , row_number() over(partition by APPLYID,REPREVEH_DICTID,year(OBVTM) order by OBVTM desc) as sequence 
from S_Axleload ) a  where sequence =1 {0} ORDER BY obvtm asc;", applyIdWhere);
            IDbConnection conn = Adapter.OpenDbConnection();
            return conn.Select<T>(sql);
        }

        public IEnumerable<T> GetAxleloadBySeg<T>(string rdwayid, double beginmp, double endmp, int direct, double? ver)
                where T : class, IS_Axleload
        {
            string sql = string.Format(@"select b.*
from S_APPLY  a 
join S_AXLELOAD b
on a.APPLYID==b.APPLYID
where a.APPRDID='{0}' and 
	a.APPBEGINMP<={1} and 
	a.APPENDMP>=2} and 
	a.DIRECTION_DICTID={3} and
	b.HIS_VERSION={4};", rdwayid, beginmp, endmp, direct, ver);
            IDbConnection conn = Adapter.OpenDbConnection();
            return conn.Select<T>(sql);
        }
    }
}
